#!/bin/bash
#function: auto update hostname and ip 
#parmeter:
#author:QQ群496379594
#version: 1.1
#Test: CentOS6 CentOS7

forCentOS6 () {
read -p "input hostname " hname
oldname=`grep HOSTNAME /etc/sysconfig/network|cut -d "=" -f 2`
sed -i  "s/$oldname/$hname/" /etc/sysconfig/network


cd /etc/sysconfig/network-scripts/

nicuse=`ls -l ifcfg-eth{0..9}|awk '{print $9}'|head -1`

nicuseif=`ifconfig -a|grep eth|head -1|awk '{print $1}'`

newmac=`ifconfig $nicuseif|grep HW|awk '{ print $5 }'`

oldmac=`grep HW /etc/sysconfig/network-scripts/$nicuse | awk -F '=' '{ print $2}'`
sed -i "s/$oldmac/$newmac/"  /etc/sysconfig/network-scripts/$nicuse


read -p "input new ip" nip

#oldip=`grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0bak |awk -F "=" '{print $2}'`
oldip=`grep IPADDR /etc/sysconfig/network-scripts/$nicuse |awk -F "=" '{print $2}'`



sed -i "s/$oldip/$nip/"  /etc/sysconfig/network-scripts/$nicuse

rm -f /etc/udev/rules.d/70-persistent-net.rules
init 6

}

forCentOS7 () {

read -p "input hostname " hname
oldname=`awk  '{print $1}' /etc/hostname`
sed -i  "s/$oldname/$hname/" /etc/hostname


cd /etc/sysconfig/network-scripts/

#获取系统当前存在网卡接口中被使用的网卡
nicuse=`ls -l ifcfg-eth{0..9} 2>/dev/null|awk '{print $9}'|head -1`

#获取克隆后当前的网卡名称
nicuseif=`ip addr|grep eth|head -1|awk '{print $2}'| awk -F: '{print $1}'`

read -p "input new ip" nip

oldip=`grep IPADDR ifcfg-eth0 |awk -F "=" '{print $2}'|tr -d '"'`
sed -i "s/$oldip/$nip/"  $nicuse

#rm -f /etc/udev/rules.d/70-persistent-net.rules
init 6

}


forCentOS7
